using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class number_box : MonoBehaviour
{
    public float bounceHeight = 0.5f;
    public float bounceSpeed = 4f;


    public float  boxLaunchX;
    public float  boxLaunchY;

    public Sprite numberSprite;
    //public boxThrow Launchbox1;
    public boxThrowRight Launchbox2;

    private Vector2 originalPosition;
    private bool countBounce = true;
    // Start is called before the first frame update
    void Start()
    {
        originalPosition = transform.localPosition;
        boxLaunchX = transform.position.x ;
        boxLaunchY = transform.position.y;
    }

    public void numberBoxBounce()
    {
        if (countBounce)
        {
            countBounce = false;
            StartCoroutine(Bounce());
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void ChangeSprite()
    {

        GetComponent<Animator>().enabled = false;
        GetComponent<SpriteRenderer>().sprite = numberSprite;
    }

    IEnumerator Bounce()
    {
        Debug.Log("11111111");
        ChangeSprite();
        Ini_box();


        while (true)
        {
            transform.localPosition = new Vector2(transform.localPosition.x, transform.localPosition.y + bounceSpeed * Time.deltaTime);
            if(transform.localPosition.y >= originalPosition.y + bounceHeight)
            {
                break;
            }
            yield return null;
        }

        while (true)
        {
            transform.localPosition = new Vector2(transform.localPosition.x, transform.localPosition.y - bounceSpeed * Time.deltaTime);
            if (transform.localPosition.y <= originalPosition.y )
            {
                transform.localPosition = originalPosition;
                break;
            }
            yield return null;
        }
    }

    void Ini_box()
    {
        /*
        Vector3 box = new Vector3(boxLaunchX , boxLaunchY  + 0.3f, transform.position.z);
        Launchbox1.GetComponent<boxThrow>().enabled = true;
        Launchbox1.GetComponent<boxpull>().enabled = false;
        Launchbox1.GetComponent<boxpull>().left = true;
        Launchbox1.GetComponent<boxpull>().right = false;
        Instantiate(Launchbox1, box, transform.rotation);
        */

        Vector3 box2 = new Vector3(boxLaunchX , boxLaunchY + 0.3f, transform.position.z);
        Launchbox2.GetComponent<boxThrowRight>().enabled = true;
        Launchbox2.GetComponent<boxpull>().enabled = false;
        Launchbox2.GetComponent<boxpull>().left = false;
        Launchbox2.GetComponent<boxpull>().right = true;
        Instantiate(Launchbox2, box2, transform.rotation);

    }

}
